<?php

Yii::import('ext.mylib.widgets.gridview.DDataColumn');

class DActiveColumn extends DDataColumn {

    public $cssClass = 'toggleActive';
    public $url;

    public function init() {
        parent::init();
        $this->registScript();
    }

    protected function renderDataCellContent($row, $data) {
        if ($data->{$this->name})
            $icon = 'ui-icon-circle-check';
        else
            $icon = 'ui-icon-circle-close';

        $htmlOptions = array(
            'class' => "haveIconHover ui-icon $icon $this->cssClass",
            'ref'   => $data->id,
        );

        echo CHtml::openTag('span', $htmlOptions);
        echo CHtml::closeTag('span');
    }

    private function registScript() {
        $gridId = $this->grid->getId();
        $msg = Yii::t('msg', 'loading');

        $script = <<<SCRIPT
$(document).ready(function() {
    $(".$this->cssClass").live('click', function(){
        var url = "$this->url" 
          + '/' + $(this).attr('ref');
        var params = {
            msg: '$msg'
        };
        $('#null').showLoading(params);

        $.post(url, null, function(data){
            $.fn.yiiGridView.update('$gridId', {
              complete: function(data){
                  $('#null').dialog('close');
              }
            });
        });
    });
});
SCRIPT;

        Yii::app()->clientScript->registerScript(
            __CLASS__ . "#{$this->cssClass}", $script, CClientScript::POS_END
        );
    }

}